package Collection01;

import java.util.ArrayList;
import java.util.Arrays;
import java.util.Collection;
import java.util.HashSet;
import java.util.Iterator;
import java.util.Random;
import java.util.function.Consumer;
import java.util.function.Predicate;

import com.teach.Student1;
import com.teach.Student2;

public class Doemo02Collection {
	public static void main(String[] args) {
		//Collection<>;//类<E>,泛型类这里表示集合内的元素的类型
		//int[] intArray;
		//Collection<int>;  编译错误,泛型类型只能是引用类型
		Collection<Integer> a1 = new ArrayList<Integer>();//接口的实例化必须使用实现的子类
		System.out.println("a1="+a1);
		a1.add(1);
		a1.add(2);
		System.out.println("a1="+a1);
		
		Collection<Integer> b2 = new HashSet<Integer>();
		System.out.println("b2="+b2);
		System.out.println("将集合a1添加到b1中:"+b2.addAll(a1)); 
		System.out.println("向b2中添加1"+b2.add(1));
		System.out.println("向b2中添加2"+b2.add(2));
		System.out.println("b2="+b2);
		
		System.out.println("清空b2");
		b2.clear();
		System.out.println("b2="+b2);
		
		System.out.println("b2="+b2);
		System.out.println("向b2中随机输入1-20的随机数");
        Random r = new Random();
        for (int i = 0; i < 20; i++) {
			a1.add(r.nextInt(20));
		}
        System.out.println("a1="+a1);
        
        System.out.println("集合中是否包含有1:"+a1.contains(1));
        System.out.println("集合中是否包含有-1:"+a1.contains(-1));
        System.out.println("集合中是否包含有10:"+a1.contains(10));
        
        Collection<Integer> c3 = Arrays.asList(new Integer[] {1,2});//企业里面大部分禁用asList
        System.out.println("集合中是否包含有[1,2]:"+a1.containsAll(c3));
        c3 = Arrays.asList(new Integer[] {1,-2});
        System.out.println("集合中是否包含有[1,-9]:"+a1.containsAll(c3));
        c3 = Arrays.asList(new Integer[] {1,2});
        System.out.println("集合中是否包含有[1,-2]:"+a1.containsAll(c3));
        
        c3=null;
        System.out.println("集合中是否为空:"+a1.isEmpty());
        System.out.println("集合中是否为空:"+b2.isEmpty());
        //System.out.println("集合中是否为空:"+c3.isEmpty());//java.lang.NullPointerException
        
        a1.add(1);
        System.out.println("a1="+a1);
        System.out.println("删除a1中的1:"+a1.remove(1));
        System.out.println("a1="+a1);
        System.out.println("删除a1中的1:"+a1.remove(1));
        System.out.println("a1="+a1);
        
        Collection<Student1> d4 = new ArrayList<Student1>();
        Student1 s1= new Student1("张三",1l);
        d4.add(s1);
        d4.add(new Student1("张三",1l));
        d4.add(s1);
        d4.add(new Student1("李四",1l));
        d4.add(new Student1("李四",1l));
        System.out.println("c4="+d4);
        System.out.println("c4是否包含新对象张三:"+d4.contains(new Student1("张三",1l)));
        System.out.println("c4是否包含s1对象张三:"+d4.contains(s1));
        System.out.println("c4中删除含新对象张三:"+d4.remove(new Student1("张三",1l)));
        System.out.println("c4是否删除s1对象张三:"+d4.remove(s1));
        System.out.println("d4="+d4);
        
        
        Collection<Student2> c5 = new ArrayList<Student2>();
        Student2 s2= new Student2("张三",1l);
        c5.add(s2);
        c5.add(new Student2("张三",1l));
        c5.add(s2);
        c5.add(new Student2("李四",1l));
        c5.add(new Student2("李四",1l));
        System.out.println("c5="+c5);
        System.out.println("c5是否包含新对象张三:"+c5.contains(new Student2("张三",1l)));
        System.out.println("c5是否包含s2对象张三:"+c5.contains(s2));
        System.out.println("c5中删除含新对象张三:"+c5.remove(new Student2("张三",1l)));
        System.out.println("c5是否删除s2对象张三:"+c5.remove(s2));
        System.out.println("c5="+c5);
        
        //结论:集合的collection的方法和remove[removeAll]方法中是使用equals方法判断两个对象是否一致的
        
        
        System.out.println("a1="+a1);
        System.out.println("a1.size()"+a1.size());
        
        Object[] is = a1.toArray();
        System.out.println("os="+Arrays.toString(is));
        Integer[] os = a1.toArray(new Integer[0]);
        System.out.println("os="+Arrays.toString(os));
       // int[] is1 = a1.toArray(new int[0]);//不能直接转成对应的基本数据类型数组
        
        //条件删除
       a1.removeIf(new Predicate<Integer>() {
		@Override
		public boolean test(Integer t) {	
			return t%2==0;
		}   
	});
       a1.removeIf(t->t%3==0);//lambda
       System.out.println("a1="+a1);
       
       
       System.out.println("使用增强型for循环");
       for(Integer item : a1) {
    	   System.out.println(item);
       }
       //增强型for循环能遍历:数组,集合,实现了java.lang.Iterable的类
       
       System.out.println("使用迭代器遍历");
       Iterator<Integer> it = a1.iterator();
       while(it.hasNext()) {
    	   Integer item = it.next();
    	   System.out.println(item);
       }
       System.out.println("forEach方法");
       a1.forEach(new Consumer<Integer>() {

		@Override
		public void accept(Integer t) {
		System.out.println(t);
			
		}
       
       });
       System.out.println("--------------");
       a1.forEach((t)->{System.out.println(t);});//lambda
       
       
      // a1.stream();
      //jdk1.8 + 函数式变成 = lambda+stream流式  ——简单
      
	}
}
